<?php
// 文件管理逻辑类
class file_management{
	protected $path="";
	protected $totalsize=0; //目录文件大小

	//是否允许文件管理器删除目录；
	//默认为不允许 0 ,如果希望可能管理整个目录,请把值设为 1 ；
	protected $allowDeleteDir=0;

	//初始化系统
	public function __construct(){
		$path = ''; //网站根目录
		$this->path = $path;
	}


	//更改文件名
	public function renameFile($oldname,$newname){
		$oldname = $this->path.$oldname;
		$newname = $this->path.$newname;		
		if(($oldname!=$newname) && is_writable($oldname) && !file_exists($newname)){
			rename($oldname,$newname);
			return true;
		}else{
			return false;
		}
	}

	//创建新目录
	public function newDir($dirname){
		$newdir = $this->path.$dirname;
		if (!is_dir($newdir)) {
			mkdir($newdir);
		}
		if(is_writable($newdir)){
			return true;
		}else{
			return false;
		}
	}

	//移动文件
	public function moveFile($oldfile,$newfile) {
		if($oldfile!="" && $oldfile!=$newfile && !ereg("\.\.",$oldfile)){
			$oldpath = $this->path.$oldfile;
			$newpath = $this->path.$newfile;
			$newdir = dirname($newpath);			
			if (!is_dir($newdir)) mk_dir($newdir);			
			if(is_readable($oldpath) && is_writable($oldpath) && is_readable($newdir)){
				copy($oldpath,$newpath);
				unlink($oldpath);
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	//删除目录
	public function rmDirFiles($indir){
		if(!is_dir($indir)){
			return false;
		}
		$dh = dir($indir);
		while($filename = $dh->read()){
			if($filename == "." || $filename == ".."){
				continue;
			}else if(is_file("$indir/$filename")){
				@unlink("$indir/$filename");
			}else{
				$this->rmDirFiles("$indir/$filename");
			}
		}
		$dh->close();
		@rmdir($indir);
		return true;
	}

	//删除文件
	public function deleteFile($filename){
		$filename = $this->path.$filename;
		if(is_file($filename)){
			@unlink($filename); //文件
			return true;
		}else{
			//目录
			if($this->allowDeleteDir==1){
				$this->rmDirFiles($filename);
				return true;
			}
			return false;
		}
	}

	//根据正则获得某目录合符规则的文件
	public function getMatchFiles($indir,$fileexp,&$filearr){
		$dh = dir($indir);
		while($filename = $dh->read()){
			$truefile = $indir.'/'.$filename;
			if($filename == "." || $filename == ".."){
				continue;
			}else if(is_dir($truefile)){
				$this->getMatchFiles($truefile,$fileexp,$filearr);
			}else if(preg_match("/\.(".$fileexp.")/i",$filename)){
				$filearr[] = $truefile;
			}
		}
		$dh->close();
		return $filearr;
	}

	//取得目录下面的文件信息
	public function listFile($pathname,$pattern='*')
	{
		if(substr($pathname, -1) != "/") $pathname .= "/";
		$dir = $dir1 = $dir2 = array();		
		$list	=	glob($pathname.$pattern);
		foreach ($list as $i=>$file){			
			$dir[$i]['filename']    = basename($file);
			$dir[$i]['pathname']    = realpath($file);
			$dir[$i]['owner']        = fileowner($file);
			$dir[$i]['perms']        = fileperms($file);
			$dir[$i]['inode']        = fileinode($file);
			$dir[$i]['group']        = filegroup($file);
			$dir[$i]['path']        = dirname($file);
			$dir[$i]['atime']        = fileatime($file);
			$dir[$i]['ctime']        = filectime($file);
			$dir[$i]['size']        = filesize($file);
			$dir[$i]['type']        = filetype($file);
			$dir[$i]['ext']      =  is_file($file)?strtolower(substr(strrchr(basename($file), '.'),1)):'';
			$dir[$i]['mtime']        = filemtime($file);
			$dir[$i]['isDir']        = is_dir($file);
			$dir[$i]['isFile']        = is_file($file);
			$dir[$i]['isLink']        = is_link($file);
			//$dir[$i]['isExecutable']= function_exists('is_executable')?is_executable($file):'';
			$dir[$i]['isReadable']    = is_readable($file);
			$dir[$i]['isWritable']    = is_writable($file);
			if ($dir[$i]['isDir']) {
				$dir1[] = $dir[$i];
			}else{
				$dir2[] = $dir[$i];
			}
		}
		$dir = array_merge($dir1,$dir2);
//		$cmp_func = create_function('$a,$b','
//			$k  =  "isDir";
//			if($a[$k]  ==  $b[$k])  return  0;
//			return  $a[$k]>$b[$k]?-1:1;
//			');
//		对结果排序 保证目录在前面
//		usort($dir,$cmp_func);
		return $dir;
	}

	//检查目录及子目录文件大小
	public function checkSize($indir){
		$dh=dir($indir);
		while($filename=$dh->read()){
			if(!ereg("^\.",$filename)){
				if(is_dir("$indir/$filename")){
					$this->checkSize("$indir/$filename");
				}else{
					$this->totalsize=$this->totalsize + filesize("$indir/$filename");
				}
			}
		}
		return $this->totalsize;
	}

	//字节格式化 把字节数格式为 B K M G T 描述的大小
	public function byteFormat($size, $dec=2){
		$a = array("B", "KB", "MB", "GB", "TB", "PB");
		$pos = 0;
		while ($size >= 1024) {
			$size /= 1024;
			$pos++;
		}
		return round($size,$dec)." ".$a[$pos];
	}

}

?>